package com.lgc.kt_jetpack_android_edu

import kotlinx.coroutines.Deferred
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.withTimeoutOrNull
import org.junit.Test

import org.junit.Assert.*
import kotlin.system.measureTimeMillis

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
//        testlaunch()
//        testasync()
//        testRunblocking()
//        testTimeOut()
        testAwait()//重要
    }


    /**
     * 1，launch 异步， 不阻塞线程
     * CoroutineScope.launch 具有生命周期
     * CoroutineScope.async 异步，不阻塞线程，具有生命周期
     * 协程返回值调用: 协程A.join()协程A执行完才执行后面的--------------；  协程B.cancel()取消协程
     * */
    fun testlaunch() {//返回值不一样，job
        val time: Long = measureTimeMillis {
            val job=GlobalScope.launch {
                Thread.sleep(1000)
                System.out.println("testlaunch的launch中,${Thread.currentThread()} 我很好")
            }
            val job2=GlobalScope.launch {
                Thread.sleep(1000)
                System.out.println("testlaunch的launch中2,${Thread.currentThread()} 我很好111")
            }
            System.out.println("test,${Thread.currentThread()} 2222")
            System.out.println("你好吗?${Thread.sleep(2200)}")
        }
        System.out.println("函数总耗时:$time")
    }

    /***
     * 异步，不阻塞线程，具有生命周期
     */
    fun testasync() {//返回值不一样，deferred
        val time: Long = measureTimeMillis {
            val deferred=GlobalScope.async {
                Thread.sleep(1000)
                System.out.println("testlaunch的launch中,${Thread.currentThread()} 我很好")
            }
            val deferred2=GlobalScope.async {
                Thread.sleep(1000)
                System.out.println("testlaunch的launch中2,${Thread.currentThread()} 我很好111")
            }
            System.out.println("test,${Thread.currentThread()} 2222")
            System.out.println("你好吗?${Thread.sleep(2200)}")
        }
        System.out.println("函数总耗时:$time")
    }

    /**
     * 阻塞线程
     * 桥接普通函数和协程
     */
    fun testRunblocking(){
        val time: Long = measureTimeMillis {
            runBlocking {
                println("testrunblock的block中${Thread.currentThread()} before delay")
                delay(500)
                println("testrunblock的block中${Thread.currentThread()} before after")
            }
            println("testrunblock的block中${Thread.currentThread()} hello")

        }
    }

    /**
     * 在规定的时间内执行完成，若没执行完成就执行后面的逻辑
     */
    fun testTimeOut()= runBlocking {
        withTimeout(3000){//在3000毫秒内执行完成，超时自动取消内部协程，抛异常
            launch {
                repeat(20){
                    //TODO
                    println("withtimeout${Thread.currentThread()}$it")
                }
            }
            println("withtimeout${Thread.currentThread()}")
        }
        withTimeoutOrNull(1000){//异步会执行这个
            //TODO
        }
    }

    /**
     * 重要
     */
    fun testAwait()= runBlocking {
        val time:Long= measureTimeMillis {
            val a2: Deferred<Int> = async {
                println("testAWait ${Thread.currentThread()} a2很好")
                delay(1000)
                100
            }
            val a1: Deferred<Int> = async {
                println("testAWait ${Thread.currentThread()} a1很好")
                delay(1000)
                99
            }
            println("结果:${Thread.currentThread()} -- ${a1.await()}--${a2.await()}")
        }
        println("总耗时:$time")
    }
}